package com.zhugang.week01;

/**
 * @program algorithms
 * @description: replaceSpace
 * @author: chanzhugang
 * @create: 2022/05/24 23:16
 */
public class ReplaceSpace {

    /**
     * 剑指offer 05. 替换空格
     *
     * @param s
     * @return
     */
    public String replaceSpace(String s) {
        // 遍历字符串，如果字符是空格，替换为"%20"
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ') {
                stringBuilder.append(s.charAt(i));
            } else {
                stringBuilder.append("%20");
            }
        }
        return stringBuilder.toString();
    }

    public String replaceSpace2(String s) {
        String replace = s.replace(" ", "%20");
        return replace;
    }
}